<template>
  <el-container style="height: 950px; border: 1px solid #eee">
    <el-aside width="350px" >
      <el-container >
        <!--   患者选择区      -->
        <el-header style="padding: 0;margin: 0">
          <el-row>
            <el-col :span="21" style="text-align: left">
              <el-tag>患者选择：</el-tag>
            </el-col>
            <el-col :span="3" style="text-align: right">
              <el-button type="primary"  size="small" @click="reloadUser" style="margin-right: 5px">
                <el-icon><Refresh/></el-icon>
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-input placeholder="请输入内容" v-model="inputPatient"  style="width: 100%">
              <!--@slot 插槽前方添加内容-->
              <template #prepend>
                患者名：
              </template>
              <!--@slot 插槽后方添加内容-->
              <template #append>
                <el-button type="primary"  size="small" @click="checkwithname" >
                  <el-icon><Search/></el-icon>
                </el-button>
              </template>
            </el-input>
          </el-row>
        </el-header>
        <el-main style="height: 800px;padding: 0;margin: 0">
          <el-tabs type="border-card"  >
            <el-tab-pane label="科室" >
              <el-tag>未检验患者：</el-tag>
              <el-table ref="singleTable" :data="MyPatientsData01" highlight-current-row
                        style="width: 100%;height: 50%" :show-header="false" size="small"
                        @row-dblclick = "handleDBCurrentChange">
                <el-table-column prop="CaseNumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column prop="RealName" label="姓名" >
                </el-table-column>
                <el-table-column prop="Name" label="项目" >
                </el-table-column>
              </el-table>
              <el-tag>已检验患者：</el-tag>
              <el-table ref="singleTable" :data="MyPatientsData02"
                        style="width: 100%;height: 50%" :show-header="false" size="small"
                        @row-dblclick = "handleDBCurrentChange">
                <el-table-column prop="CaseNumber" label="病历号" width="80">
                </el-table-column>
                <el-table-column prop="RealName" label="姓名" >
                </el-table-column>
                <el-table-column prop="Name" label="项目" >
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-main>
      </el-container>
    </el-aside>

    <el-container direction="vertical" style="height: 950px">
      <el-row>
        <span style="margin-left: 20px;font-size: small;" :span="20">{{showPatientsTop}}</span>
        <el-button type="text" size="small" style="float: right;margin-right: 20px;" @click="endRegister" v-if="showStop" :span="2">诊毕</el-button>
      </el-row>

<!--      检验-->
      <el-tabs type="border-card" :before-leave="toTab" >
        <el-tab-pane label="执行检验" >
          <!-- 1. 门诊病历 -->
          <el-container v-loading="loading">
            <!-- 页面正文 -->
            <el-main style="width:100%;background:#fff;height:800px;overflow-y: auto">
              <el-form ref="form" v-model="checkapply" label-width="80px" label-position="left" size="small" >
                <el-row style="background-color: #EAF1F5;margin-top: -20px">
                  <el-col :span="8" style="margin-top: 4px;"></el-col>
                  <el-col :span="1" >
                    <el-button type="text" size="small"  @click="saveCheck">执行检验</el-button>
                  </el-col>
                </el-row>
                <el-tag>检验内容：</el-tag>
                <el-form-item label="项目名称">
                  <el-input type="textarea" :rows="1" v-model="checkapply.Name" readonly></el-input>
                </el-form-item>
                <el-form-item label="项目目的要求">
                  <el-input type="textarea" :rows="4" v-model="checkapply.Objective" readonly></el-input>
                </el-form-item>
                <el-form-item label="是否加急">
                      <span v-if="checkapply.IsUrgent">是</span>
                      <span v-else>否</span>
                </el-form-item>
                <el-form-item label="状态">
                      <span v-if="checkapply.State==2">待执行</span>
                      <span v-if="checkapply.State==4">已完成</span>
                      <span v-if="checkapply.State==3">已执行</span>
                      <span v-if="checkapply.State==5">取消</span>
                      <span v-if="checkapply.State==1">未缴费</span>
                </el-form-item>
              </el-form>
              <el-button type="text" size="small"  @click="saveCheck">执行检验</el-button>
            </el-main>
            <el-dialog v-model="DialogFormVisible" title="收费确认" width="500">
              <el-form :model="editForm" label-width="auto"  >
                <el-form-item label="检验结果:">
                  <el-input placeholder="请输入检验结果" v-model="editForm.result"  style="width: 100%"></el-input>
                </el-form-item>
                <el-form-item >
                  <el-button type="primary"  @click="charge" >确认</el-button>
                  <el-button @click="DialogFormVisible = false">取消</el-button>
                </el-form-item>
              </el-form>
            </el-dialog>
          </el-container>
        </el-tab-pane>

      </el-tabs>
    </el-container>
  </el-container>
</template>


<script setup>
import {onMounted, ref,computed} from 'vue'
import {ElMessage, ElNotification} from 'element-plus'
import service from '@/utils/request'
import {Search} from "@element-plus/icons-vue";
import {useCounterStore}  from "@/stores/counter.js";
// 引入pinia
let counter = useCounterStore()
// 引入科室未诊
let MyPatientsData01 = ref()
// 引入科室已诊
let MyPatientsData02 = ref()
//病历首页
let medicalRecord01 = ref({})
let showPatientsTop = ref("请选择患者")
let casenumber = ref('')
let realname = ref('')
let xiangmu = ref('')
let id = ref('')
let inputPatient = ref('')

//刷新患者列表
function reloadUser(){
  listData(null)
  inputPatient.value = ''
  casenumber.value = ''
  realname.value = ''
  id.value = ''
  showPatientsTop.value = '请选择患者'
  ElMessage.success('刷新成功')
}
// 查询患者姓名模糊查询
function checkwithname(){
  listData(inputPatient.value)
}
let checkapply = ref({})
//双击选中项目
function handleDBCurrentChange(row){   //双击选中患者
  casenumber.value = row.CaseNumber
  realname.value = row.RealName
  id.value = row.ID
  xiangmu.value = row.Name
  showPatientsTop.value = "病历号:" + casenumber.value + "  姓名:" + realname.value + "  项目:" + xiangmu.value
  checkapply.value = row
}
let DialogFormVisible = ref(false)
let editForm = ref({})
function saveCheck(){
  editForm.value ={}
  DialogFormVisible.value = true
}
function charge(){
  service.post("/checkapply/changes?id="+id.value+"&result="+editForm.value.result+"&operid="+counter.vo_user.value.id).then(
      res =>{
        if(res.data){
          ElNotification({
            title: '提示',
            message: '检验执行成功',
            type: 'success',
          })
          DialogFormVisible.value = false
          listData( null)
          checkapply.value ={}
        }else{
          ElNotification({
            title: '提示',
            message: '检验执行失败',
            type: 'warning',
          })
        }
      }
  )
}

let mydept = ref('')
async function listData(a){
  //先确定自己科室
  const data = await service.get("/dept/getbyid?deptid="+counter.vo_user.value.deptid)
  mydept.value = data.data.deptname
  //找出自己科室的待检验项目
  service.get("/checkapply/getbydeptname?deptname="+mydept.value+"&state=2" +"&realname="+a).then(res =>{
    MyPatientsData01.value = res.data.list
  })
  //找出自己科室的已经完成检验
  service.get("/checkapply/getbydeptname?deptname="+mydept.value+"&state=4" +"&realname="+a).then(res =>{
    MyPatientsData02.value = res.data.list
  })
}
onMounted(() => {
 listData(null)
})
</script>